package main.com.oaer.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import main.com.oaer.model.Purview;
import main.com.oaer.model.User;
import main.com.oaer.service.IndexService;
import main.com.oaer.service.UserService;
import main.com.oaer.util.CommonUtil;

/**
 * index 控制器
 */
@Controller
@RequestMapping(value="/index")
public class IndexAction {

	@Autowired
	IndexService indexService;	
	
	@Autowired
	UserService userService;
	
	/**
	 * 转到登录页
	 * @return  "login" 登录页
	 */
	@RequestMapping
	public String toLogin(){
		return "login";
	}
	
	/**
	 * 登出
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute("firstPurviewList");
		session.removeAttribute("userMap");
		session.invalidate();
		return "login";
	}
	
	/**
	 * 登录验证，成功后获取用户的一级、二级、三级菜单
	 * @param username 用户名
	 * @param password 密码
	 * @param map 模型map
	 * @param session 会话
	 * */
	@RequestMapping(value="/login")
	public String doLogin(String username, String password,ModelMap map,HttpSession session){
		User user = userService.findUserByUsername(username);
		//该用户存在且密码正确
		if(user!=null && 
				password!=null && 
				  password.equals(user.getPassword())){
			session.setAttribute("userMap", user);
			//取该用户的所有一级菜单
			List<Purview> firstPurviewList = indexService.findFirstPurviewByUserId(user.getId());
			session.setAttribute("firstPurviewList", firstPurviewList);
			//取第一个一级菜单下的所有二级菜单
			if(firstPurviewList !=null && firstPurviewList.size()>0){
				Purview firstPurview = firstPurviewList.get(0);
				List<Purview> secondPurviewList = indexService.findUserPurviewByParentId(user.getId(), firstPurview.getId());
				map.put("secondPurviewList", secondPurviewList);
				//取第一个一级菜单下的所有二级菜单的对应的所有三级菜单
				Map<Long,List<Purview>> thirdPurviewListMap = new HashMap<Long,List<Purview>>();
				for(Purview purview:secondPurviewList){
					 List<Purview> thirdPurviewList = indexService.findUserPurviewByParentId(user.getId(), purview.getId());
					 thirdPurviewListMap.put(purview.getId(), thirdPurviewList);
				}
				map.put("thirdPurviewListMap", thirdPurviewListMap);
			}
			map.put("firstPurviewList", firstPurviewList);
			map.put("user", user);
			return "index";
		}else{
			map.put("msg", "用户名或密码错误！");
			return "login";
		}
	}
	
	/**转到用户指定菜单页
	 * @param id 一级菜单的id
	 * @param map 模型map
	 * @param session 会话
	 * @return “index” 用户主页
	 * */
	
	@RequestMapping(value="/{id}")
	public String getPurviews(@PathVariable Long id, ModelMap map,HttpSession session){
		if(id != null){
			User user = CommonUtil.getUserFromSession(session);
			//取指定的一级菜单下的所有二级菜单
			List<Purview> secondPurviewList = indexService.findUserPurviewByParentId(user.getId(), id);
			map.put("secondPurviewList", secondPurviewList);
			//取指定的一级菜单下的二级菜单的对应的三级菜单
			Map<Long,List<Purview>> thirdPurviewListMap = new HashMap<Long,List<Purview>>();
			for(Purview purview:secondPurviewList){
				List<Purview> thirdPurviewList = indexService.findUserPurviewByParentId(user.getId(), purview.getId());
				thirdPurviewListMap.put(purview.getId(), thirdPurviewList);
			 }
			map.put("thirdPurviewListMap", thirdPurviewListMap);
			//取该用户的所有一级菜单
			List<Purview> firstPurviewList = (List<Purview>) session.getAttribute("firstPurviewList");
			if(firstPurviewList==null){
				 firstPurviewList = indexService.findFirstPurviewByUserId(user.getId());
			}
			map.put("firstPurviewList", firstPurviewList);
			return "index";
		}else{
			return null;
		}
	}
	
}
